package com.peanut.youfupay.controller;


import com.alibaba.fastjson.JSON;
import com.peanut.youfupay.entity.response.ResponseMessage;
import com.peanut.youfupay.service.BaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 优付充值结果异步通知
 * 1、充值只有成功才会进行异步通知
 * 2、通知逻辑：累计5次，商户返回SUCCESS则停止
 * 每隔5秒通知1次，共1次
 * 每隔30秒通知1次，共2次
 * 每隔60秒通知1次，共2次
 *
 * @author peanut
 */
@Controller
@RequestMapping("/recharge")
@Slf4j
public class RechargeNotifyController {
    @Autowired
    BaseService baseService;

    @PostMapping(value = "/notify")
    @ResponseBody
    public String doBusiness(@RequestBody String encryptedMsg) {

        log.info("优付充值异步通知返回报文:" + encryptedMsg);
        ResponseMessage encryptedResponseMessage = JSON.parseObject(encryptedMsg, ResponseMessage.class);
        ResponseMessage responseMessage = baseService.handleResponse(encryptedResponseMessage);
        String notifyData = responseMessage.getResData();
        log.info("优付充值异步通知返回结果-notifyData:" + notifyData);
        //返回给优付的结果
        return "SUCCESS";
    }

    /**优付充值异步通知返回完整报文
     * 商户可用此报文作为请求报文用postMan发给异步通知controller测试
     * Content-Type=application/json
     * /

     /**
     {"funCode":"6021","merId":"89900000300716159604","resCode":"0000",
     "resData":"giko4kmkxHapp3wwaxNd9oyfLJsovgIMj5th4DCUYzDL9yOdPworyRPYZgHRYB358r04SvxGeTqKd79NB1tCLHL6Nw/U6z4ThCp1rvh+tm13rg0+Z8213BMm1aqMZAq6tU/t/NGcHO4pfc+pyc+XusmLfaB5CFvXe75kN2p6Fe8SRLmaj8hkQ/N8ieypwiKO4eT+4FyV1+o=",
     "resMsg":"成功",
     "sign":"AqWaKTo8xcLC3+zwbMJ9j23QyJQKgSxXYM5fY1y0QmNiRcguDxhjancgRMjRyIfZX7nxnQ2jM25hkzu575Cn8T7USjDYQ9B1jtuZsznh8nHOW1sOlTqCMUDnWYWHjeWCRoAzDY7rtduOuTSL6r4zGkGjcuWDhbuYi7QFUDoa1nk=","version":"V1.0"}
     */

}
